using RimWorld;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEngine;
using Verse;

namespace RimZombieGirl
{
    // 参考 Need_Beauty
    // 参考 Need_Suppression
    public class Need_Zombie_Suppress : Need_Seeker
    {
        // 默认下降影响因子（每个Tick下降量）
        private const float DownPerTickBase = 0.00025f;
        // 每小时失控几率
        private const float UpPerHourChaos = 0.1f;
        // 僵尸失控程度分级（0.0=完全狂暴 → 1.0=彻底镇压）
        public const float ThreshChaosUnbound = 0.10f;  // 即将失控
        public const float ThreshWeakControl = 0.35f;  // 约束微弱
        public const float ThreshSealStruggle = 0.55f;  // 封印松动
        public const float ThreshFirmRestrain = 0.75f;  // 稳固压制
        public const float ThreshAbsoluteLock = 0.90f;  // 完全镇压

        // 上一次即时检测的tick
        private int lastInstantCheckTick = -9999;
        // 上一次即时值（缓存，避免每tick重复计算）
        private float lastInstantValue = -1f;
        // 上次满足
        private int lastSatisfiedTick = 0;
        // 上次失控检测
        private int lastChaosCheckTick = 0;
        public InteractionDef lastInteractionDef;

        // 当前即时需求等级（根据当前美观环境计算，带缓存优化）
        public override float CurInstantLevel
        {
            get
            {
                // 每60Tick重新计算
                if (lastInstantCheckTick < Find.TickManager.TicksGame - 60)
                {
                    // 未生成（未上地图）默认中性
                    if (!pawn.Spawned)
                    {
                        lastInstantValue = ThreshSealStruggle;
                    }
                    else
                    {
                        // TODO 这里需要修改
                        lastInstantValue = ThreshSealStruggle;
                    }
                    lastInstantCheckTick = Find.TickManager.TicksGame;
                }
                return lastInstantValue;
            }
        }

        public int LastSatisfiedTick { get; set; }
        // 必须紧急镇压
        public bool MustSuppress
        {
            get
            {
                return CurLevel < ThreshChaosUnbound;
            }
        }

        // 需要镇压
        public bool ShouldSuppress
        {
            get
            {
                return CurLevel < ThreshSealStruggle;
            }
        }

        // 如果角色未生成，则需求被冻结
        protected override bool IsFrozen => !pawn.Spawned;


        // 构造函数，初始化阈值列表（用于UI显示分档线）
        public Need_Zombie_Suppress(Pawn pawn)
            : base(pawn)
        {
            threshPercents = new List<float>
            {
                ThreshChaosUnbound,
                ThreshWeakControl,
                ThreshSealStruggle,
                ThreshFirmRestrain,
                ThreshAbsoluteLock
            };
        }

        // 需求间隔调用（每150tick），处理饥饿消耗与营养不良
        public override void NeedInterval()
        {
            // 非冻结状态下，减少镇压值
            if (!IsFrozen)
            {
                CurLevel -= DownPerTickBase * pawn.GetStatValue(ZombieDefOf.RI_Zombie_Stat_SuppressDownRate);
            }
            // 每小时判断一次失控
            if (Find.TickManager.TicksGame >= lastChaosCheckTick + 2500)
            {
                lastChaosCheckTick = Find.TickManager.TicksGame;
                
                // 每次判断时有一定概率触发失控
                if (Rand.Chance(UpPerHourChaos))
                {
                    // 失控，触发狂暴互动
                    if (pawn.Spawned && !pawn.Downed && !pawn.InMentalState && !pawn.IsBurning())
                    {
                        // 触发猎杀人类
                        pawn.mindState.mentalStateHandler.TryStartMentalState(MentalStateDefOf.Manhunter, null, false, false, false, null);
                    }
                }
            }
        }

        public override void ExposeData()
        {
            Scribe_Values.Look(ref lastSatisfiedTick, "lastSatisfiedTick", 0);
        }


    }

}